package respository

import (
	"gitee.com/song-yao-shun/MQH/common"
	"gitee.com/song-yao-shun/MQH/dto"
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/vo"
	"gorm.io/gorm"
	"log"
)

//定义接口和定义增删改查的方法
type TCaseRItf interface {
	Create(tc model.TCase)
	Update(tc model.TCase)
	SelectById(Id int)
	SelectFullTCaseById(Id int)
	DeleteById(Id int)
	List(tcs vo.TCases)
}

type TCaseRepository struct {
	DB *gorm.DB
}

func (T TCaseRepository) Create(tc model.TCase) (int, error){

	if err := T.DB.Create(&tc).Error; err != nil {
		log.Printf("新增失败，错误信息为：%v",err)
		return 0, err
	}

	return tc.ID, nil
}

func (T TCaseRepository) Update(tc model.TCase) (*model.TCase, error){

	if err := T.DB.Updates(&tc).Error;err != nil {
		return nil, err
	}

	return &tc,nil
}
func (T TCaseRepository) SelectById(Id int) (tc *model.TCase,err error){

	if err := T.DB.Where("id = ?",Id).First(&tc).Error; err != nil{
		return nil, err
	}

	return
}

func (T TCaseRepository) SelectFullTCaseById(Id int)  *dto.FullTCaseDto{

	T.DB = T.DB.Where("t_cases.id = ?", Id)
	selectSql := "t_cases.id,t_cases.name,t_cases.node_id as node_id," +
		"n.name as node_name,t_cases.order_num,t_cases.priority,t_cases.precondition," +
		"t_cases.test_data,t_cases.procedure,t_cases.expected_result,t_cases.img,t_cases.desc," +
		"t_cases.created_at,t_cases.updated_at"
	joinsSql := "left join nodes as n on t_cases.node_id = n.id "


	var fullTCase dto.FullTCaseDto

	T.DB.Model(&model.TCase{}).Select(selectSql).Joins(joinsSql).Scan(&fullTCase)

	return &fullTCase
}

func (T TCaseRepository) DeleteById(Id int) (rowsAffected int64) {

	rowsAffected = T.DB.Where("id = ?", Id).Delete(model.TCase{}).RowsAffected
	if rowsAffected != 1 {
		log.Printf("数据删除失败，删除操作影响行数为：%v，删除的id为：%v",rowsAffected,Id)
	}

	return
}

func (T TCaseRepository) List(tcs vo.TCases) (tcases []dto.FullTCaseDto, total int64){
	// 条件查询
	if tcs.ID > 0  {
		T.DB = T.DB.Where("t_cases.id = ?", tcs.ID)
	}
	if tcs.Name != ""  {
		T.DB = T.DB.Where("t_cases.name like ?", "%" + tcs.Name + "%")
	}
	if tcs.NodeId > 0  {
		T.DB = T.DB.Where("t_cases.node_id = ?", tcs.NodeId)
	}
	if tcs.Priority != ""  {
		T.DB = T.DB.Where("t_cases.priority = ?", tcs.Priority)
	}
	if tcs.PageNum == 0  {
		tcs.PageNum = 1
	}
	if tcs.PageSize == 0  {
		tcs.PageSize = 10
	}

	selectSql := "t_cases.id,t_cases.name,t_cases.node_id as node_id," +
		"n.name as node_name,t_cases.order_num,t_cases.priority,t_cases.precondition," +
		"t_cases.test_data,t_cases.procedure,t_cases.expected_result,t_cases.img,t_cases.desc," +
		"t_cases.created_at,t_cases.updated_at"
	joinsSql := "left join nodes as n on t_cases.node_id = n.id "

	//总记录数
	T.DB.Model(&model.TCase{}).Count(&total)

	T.DB.Model(&model.TCase{}).Select(selectSql).
		Joins(joinsSql).
		Order("t_cases.created_at desc").
		Offset((tcs.PageNum -1) * tcs.PageSize).
		Limit(tcs.PageSize).Scan(&tcases)

	return
}

func NewTCaseRepository() TCaseRepository{
	db := common.GetDB()
	//db.AutoMigrate(model.TCase{})

	return TCaseRepository{DB: db}
}